package com.shop.trade.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.entity.trade.OrderDetail;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderDetailInput;
import com.shop.common.model.result.trade.OrderDetailOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.trade.atom.atomsvc.IOrderDetailAtomSvc;
import com.shop.trade.atom.dao.OrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月05日  19:02
 */
@Service
public class OrderDetailAtom extends BaseService implements IOrderDetailAtomSvc {
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public ServiceBean<Integer> insertOrderDetail(OrderDetail orderDetailInput) {
        int i = orderDetailMapper.insertSelective(orderDetailInput);
        if (i > 0) {
            return resultData(orderDetailInput.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<Integer> insertOrderDetails(List<OrderDetail> orderDetails) {
        int i = orderDetailMapper.insertList(orderDetails);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteOrderDetailById(Integer id) {
        int i = orderDetailMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderDetailOutput>> selectOrderDetailByOrderId(Integer orderId) {
        List<OrderDetailOutput> orderDetails = orderDetailMapper.selectByOrderId(orderId);
        if (orderDetails != null && orderDetails.size() > 0) {
            return resultData(orderDetails);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderDetailOutput>> selectOrderDetailList(OrderDetailInput orderDetailInput) {
        List<OrderDetailOutput> list = orderDetailMapper.qryOrderDetails(orderDetailInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderDetailOutput>> selectOrderDetailList4P(InputBean<OrderDetailInput> inputBean) {
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<OrderDetailOutput> list = orderDetailMapper.qryOrderDetails(inputBean.getParam());
        ServiceBean<List<OrderDetailOutput>> serviceBean = new ServiceBean<>();
        if (list != null && list.size() > 0) {
            PageInfo<OrderDetailOutput> pageInfo = new PageInfo<>(list);
            serviceBean.setSuccess(true);
            serviceBean.setData(pageInfo.getList());
            serviceBean.setMaxSize(pageInfo.getTotal());
        } else {
            serviceBean.setSuccess(false);
        }
        return serviceBean;
    }

    @Override
    public ServiceBean<String> updateOrderDetails(OrderDetailInput orderDetailInput) {
        int i = orderDetailMapper.updateByPrimaryKeySelective(orderDetailInput);
        if (i > 0) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public int qryOrderDetailCount(OrderDetailInput input) {
        return orderDetailMapper.qryOrderDetailCount(input);
    }
}
